"use strict";

const Controller = require("egg").Controller;
const jwt = require("jsonwebtoken");
// const fs = require("fs");
const Mock = require("mockjs");

const newCityData = require("../../mock.json");
const houseData = require("./data.json");
const { groupData, liveData, data } = require("../../mock/index");

class HomeController extends Controller {
    // 插入城市
    async index() {
        const { ctx, app } = this;
        const data = await app.mysql.insert("city", newCityData);
        ctx.body = {
            msg: "h1,egg",
            data,
        };
    }

    // 给城市数据添加一个字段
    async updateCityData() {
        const { ctx, app } = this;
        const context = Mock.mock("@natural(10000, 30000)");
        const data = await app.mysql.query(
            `update city set context="${context}" where level=3`
        );
        ctx.body = {
            msg: "h1,eg11g",
            data,
        };
    }

    // 返回所有三级城市
    async threeCity() {
        const { ctx, app } = this;
        const { level } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from city where level=${level}`
        );
        ctx.body = {
            msg: "h1,eg11g",
            data,
        };
    }

    // 获取城市列表数据
    async getCityData() {
        const { ctx, app } = this;
        const data = await app.mysql.query(
            `select * from city where level=1 and name like "%市%" or name like "%台湾省%" or name like "%行政区%" and name not like "%直辖%" or level like "2"`
        );

        ctx.body = {
            code: 200,
            data,
        };
    }

    // 插入房源信息;
    // async insert() {
    //     const { ctx, app } = this;
    //     await data.forEach((item) => {
    //         app.mysql.insert("houselist", item);
    //     });
    //     ctx.body = {
    //         msg: "h1,egg",
    //         data,
    //     };
    // }

    // 获取pv大于500的 也就是推荐的数据
    async getHouseData() {
        const { ctx, app } = this;
        const { classify } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from houselist where classifyType=${classify} and pv>500`
        );
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 通过classify类型 判断数据 1 2 3 4 5 6
    async getRentHouseList() {
        const { ctx, app } = this;
        const { classify, values } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from houselist where classifyType=${classify} and listingTitle like '%${values}%' `
        );
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 获取用户信息
    async getUserInfo() {
        const { ctx, app } = this;
        const { token } = ctx.request.body;
        const newToken = jwt.verify(token, "sx");
        const data = await app.mysql.get("login", { phone: newToken });
        ctx.body = {
            code: "200",
            data,
        };
    }

    // 筛选地区
    async getAreaData() {
        const { ctx, app } = this;
        // const { area } = ctx.request.body;
        const data = await app.mysql.select("houselist");
        const newData = Array.from(new Set(data.map((item) => item.area)));
        ctx.body = {
            code: "200",
            data: newData,
        };
    }

    // 筛选地区 模糊搜索
    async searchArea() {
        const { ctx, app } = this;
        const { area } = ctx.request.body;

        const data = await app.mysql.query(
            `select * from houselist where area='${area}'`
        );

        ctx.body = {
            code: "200",
            data,
        };
    }

    // 详情接口
    async getDeatailList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const data = await app.mysql.get("houselist", { id });
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 筛选 价格
    async searchPrice() {
        const { ctx, app } = this;
        const { classify, obj } = ctx.request.body;
        let data = null;

        if (obj.a || obj.b) {
            data = await app.mysql.query(
                `select * from houselist where sellingprice<${
          obj.b * 1
        } and sellingprice>${obj.a * 1} and classifyType=${classify}`
            );
        } else {
            data = await app.mysql.query(
                `select * from houselist where  classifyType=${classify}`
            );
        }
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 筛选类型
    async searchType() {
        const { ctx, app } = this;
        const { value } = ctx.request.body;
        console.log(value);
        const data = await app.mysql.query(
            `select * from houselist where fitmentType like '%${value}%'`
        );
        console.log(data);
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 筛选地图区域
    async searchType() {
        const { ctx, app } = this;
        const { code } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from city where code like '%${code}%'`
        );
        ctx.body = {
            code: 200,
            data,
        };
    }

    // 返回所有一级城市
    async firstCify() {
        const { ctx, app } = this;
        const { level } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from city where level like ${level}`
        );
        ctx.body = {
            msg: "h1,eg11g",
            data,
        };
    }

    // 通过code 获取二级城市 和三级城市数据
    async TwoCify() {
        const { ctx, app } = this;
        const { code, level } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from city  where level=${level} and  code like '${code}%'`
        );

        ctx.body = {
            msg: "h1,eg11g",
            data,
        };
    }

    // 分页
    async page() {
        const { ctx, app } = this;
        const { classify, current, size } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from houselist  where classifyType=${classify} limit ${
        (current - 1) * size
      },${size}`
        );
        const newData = await app.mysql.query(
            `select * from houselist  where classifyType=${classify}`
        );
        ctx.body = {
            msg: "h1,eg11g",
            data,
            total: newData.length,
        };
    }

    // 团购list
    async getGroupList() {
        const { ctx, app } = this;

        const data = await app.mysql.select("grouplist");

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    // 直播list
    async getLiveList() {
        const { ctx, app } = this;
        const { size, current } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from live limit ${(current - 1) * size},${size}`
        );
        const newdata = await app.mysql.select("live");
        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
            total: newdata.length,
        };
    }

    // 直播管理的搜索
    async searchLiveList() {
        const { ctx, app } = this;
        const { value } = ctx.request.body;
        console.log(value);
        const data = await app.mysql.query(
            `select * from live where name like '%${value}%' `
        );

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    //  添加团购数据
    async addGroup() {
        const { ctx, app } = this;

        await groupData.forEach((item) => {
            app.mysql.insert("grouplist", item);
        });

        ctx.body = {
            code: 1,
        };
    }

    // 添加直播数据
    // async addLive() {
    //     const { ctx, app } = this

    //     await liveData.forEach(item => {
    //         app.mysql.insert("live", item)
    //     })

    //     ctx.body = {
    //         code: 1,
    //     }
    // }

    // 直播list
    async addHouseList() {
        const { ctx, app } = this;
        await app.mysql.insert("houselist", {...ctx.request.body });
        ctx.body = {
            code: 200,
            msg: "添加成功",
        };
    }

    // 删除房源
    async delHouseList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        await app.mysql.delete("houselist", { id });
        ctx.body = {
            code: 200,
            msg: "删除成功",
        };
    }

    // 删除直播list
    async delLiveList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        console.log(id);
        await app.mysql.delete("live", { id });
        ctx.body = {
            code: 200,
            msg: "删除成功",
        };
    }

    // 删除审核list
    async delOldHouseList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        await app.mysql.delete("oldhouse", { id });
        ctx.body = {
            code: 200,
            msg: "删除成功",
        };
    }

    // 插入二手房数据
    async insertOldHouse() {
        const { ctx, app } = this;
        await app.mysql.insert("oldhouse", {...ctx.request.body });
        ctx.body = {
            code: 200,
            msg: "添加成功",
        };
    }

    // 获取所有城市数据
    async allCity() {
        const { ctx, app } = this;
        const data = await app.mysql.query("select * from city");
        ctx.body = {
            code: 200,
            msg: "获取成功",
            data,
        };
    }

    // 获取发布二手房的数据
    async getnewoldhouse() {
        const { ctx, app } = this;
        const data = await app.mysql.select("oldhouse");
        ctx.body = {
            code: 200,
            msg: "获取成功",
            data,
        };
    }

    // 删除团购数据
    async delGroup() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        await app.mysql.delete("grouplist", { id });
        ctx.body = {
            code: 1,
            msg: "删除成功",
        };
    }

    // 房源模糊搜索
    async searchHouseList() {
        const { ctx, app } = this;
        const { value, classify } = ctx.request.body;
        console.log(value, classify);
        const data = await app.mysql.query(
            `select * from houselist where listingTitle like '%${value}%' and classifyType=${classify}`
        );

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    // 审核模糊搜索
    async searchOldList() {
        const { ctx, app } = this;
        const { value } = ctx.request.body;
        const data = await app.mysql.query(
            `select * from oldhouse where listingTitle like '%${value}%' `
        );

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    // 团购模糊搜索
    async searchGroupList() {
        const { ctx, app } = this;
        const { value, values } = ctx.request.body;

        let sql = `select * from grouplist where 1=1 `;
        if (value) {
            sql += `and floorName like '%${value}%' `;
        }
        if (values) {
            sql += `and status like '%${values}%'`;
        }
        const data = await app.mysql.query(sql);

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    // 房源下拉筛选城市
    async searchHouseCityList() {
        const { ctx, app } = this;
        const { code } = ctx.request.body;
        const data = await app.mysql.get("city", { code: code });
        const newData = await app.mysql.query(`
            select * from houselist where area like '%${data.name}%' `);

        ctx.body = {
            code: 200,
            msg: "请求成功",
            data: newData,
        };
    }

    async getvr() {
        const { ctx, app } = this;
        let data = await app.mysql.select("vr");
        data = data.map((item) => {
            return {
                ...item,
                imgs: item.imgs.split("*"),
            };
        });
        ctx.body = {
            code: 200,
            msg: "请求成功",
            data,
        };
    }

    // 用来做鉴权的pc端用户信息
    async user() {
        const { ctx, app } = this;
        const { authorization } = ctx.request.headers;
        console.log(authorization);
        let token = authorization.split("Bearer ")[1];
        token = jwt.verify(token, "sx");
        const data = await app.mysql.get("pclogin", { username: token });
        ctx.body = {
            code: "200",
            data,
        };
    }

    // 用来做鉴权的手机端用户信息
    async phoneUser() {
        const { ctx, app } = this;
        const { authorization } = ctx.request.headers;
        console.log(authorization);
        let newToken = authorization.split("Bearer ")[1];
        console.log(newToken);
        newToken = jwt.verify(newToken, "sx");
        const data = await app.mysql.get("login", { phone: newToken });
        ctx.body = {
            code: "200",
            data,
        };
    }

    // 动态创建表 聊天
    async create() {
        const { ctx, app } = this;
        const { id, ids, sf } = ctx.request.body;
        console.log(id, ids, sf);
        if (!(await app.mysql.get("allmessagelist", { title: `${id}or${ids}` }))) {
            await app.mysql.query(
                `create table ${
          id + "or" + ids
        }(id int primary key auto_increment,usernews varchar(255),identity varchar(255))`
            );
            await app.mysql.insert("allmessagelist", {
                userid: id,
                agentid: ids,
                title: `${id}or${ids}`,
            });
        }
        ctx.body = {
            code: 200,
            masg: "创建成功",
        };
    }

    // 经纪人获取聊天记录数据
    async getChatList() {
        const { ctx, app } = this;
        // agentid是身份
        const { id, identity } = ctx.request.body;
        // 拿到data 数据
        const dataOne = await app.mysql.query(`
            select * from allmessagelist where ${
              identity === "高级经纪人" ? "agentid" : "userid"
            }=${id}
        `);
        // 现在用
        const dataTwo = dataOne.map((item) => {
            return app.mysql.get("login", {
                id: item[identity === "高级经纪人" ? "userid" : "agentid"],
            });
        });
        console.log(dataTwo);

        const data = await Promise.all(dataTwo).then();
        console.log(data);

        ctx.body = {
            code: 200,
            masg: "创建成功",
            data,
        };
    }

    // 详情搜索周边信息 区域
    async periphery() {
        const { ctx, app } = this;
        const { area } = ctx.request.body;
        const data = await app.mysql.select("city", { where: { name: area } });
        ctx.body = {
            code: "200",
            data,
        };
    }

    // pc直播详情
    async liveInfo() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const data = await app.mysql.get("live", { id: id });
        ctx.body = {
            code: "200",
            data,
        };
    }
}

module.exports = HomeController;